package cn.xiu.drugstore.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.xiu.drugstore.dao.DrugTypeDao;
import cn.xiu.drugstore.pojo.DrugType;
import cn.xiu.drugstore.util.BaseDao;
/**
 * 药品种类实现类
 * @author Administrator
 *
 */
public class DrugTypeDaoImpl extends BaseDao implements DrugTypeDao {

	@Override
	public int saveType(DrugType dt) throws Exception {
		String sql="INSERT INTO drugtype(drugtype_name) VALUES(?)";
		Object[] params={dt.getDrugTypeName()};
		int result = super.executeUpdate(sql, params);
		if(result>0){
			return result;
		}
		super.closeResource(null);
		return 0;
	}

	@Override
	public List<DrugType> findAllType() throws Exception {
		ArrayList<DrugType> list=new ArrayList<DrugType>();
		String sql="SELECT drugtype_id,drugtype_name FROM drugtype";
		ResultSet rs=super.executeQuery(sql, null);
		if(rs.next()&&rs!=null){
			while(rs.next()){
				DrugType dt=new DrugType();
				dt.setDrugId(rs.getInt("drugtype_id"));
				dt.setDrugTypeName(rs.getString("drugtype_name"));
				list.add(dt);
				
				
			}
		}
		super.closeResource(rs);
		return list;
	}

	@Override
	public int updata(int id, String typeName) throws Exception {
		String sql="UPDATE drugtype SET drugtype_name=? WHERE drugtype_id=?";
		Object[] params={typeName,id};
		int	result = super.executeUpdate(sql, params);
		return result;
	}

	@Override
	public int delect(int id) throws Exception {
		String sql="DELETE FROM drugtype WHERE drugtype_id=?";
		Object[] params={id};
		int	result = super.executeUpdate(sql, params);
		return result;
	}

}
